package com.woniuxy.yogaapp.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.yogaapp.pojo.Chat;

import java.io.IOException;

/**
 * 把java对象转换成json或者把json转换成java对象的工具类
 */
public class JsonUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 把java对象转换成json字符串
     * @param object
     * @return
     */
    public static String ObjectToString(Object object) {

        try {
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 把json数据转换成Chat对象
     * @param str
     * @return
     */
    public static Chat StringToObject(String str) {

        try {
        return objectMapper.readValue(str, Chat.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
